package com.itheima.service.impl;


import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import com.itheima.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService{

    @Autowired
    private MemberDao memberDao;

    //根据手机号查询会员是否存在
    public Member findByTelephone(String telephone) {
        Member member = null;
        try {
            member = memberDao.findByTelephone(telephone);
        }catch (Exception e){
            e.printStackTrace();
        }

        return member;
    }

    //保存会员
    public void add(Member member) {
        String password = member.getPassword();
        //密码进行md5加密
        if (password != null){
            password = MD5Utils.md5(password);
            member.setPassword(password);
        }

        memberDao.add(member);
    }

    //根据月份查询当月的会员总量
    public List<Integer> findMemberCountByMonth(List<String> months) {
        List<Integer> list = new ArrayList<>();
        for (String month : months) {
            String date = month + ".31";//格式：2019.04.31
            Integer count = memberDao.findMemberCountBeforeDate(date);
            list.add(count);
        }
        return list;
    }
}
